﻿
// HackSkillDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "HackSkill.h"
#include "HackSkillDlg.h"
#include "CMyInjectionDlg.h"
#include "CServerLoadDlg.h"
#include "afxdialogex.h"
#include "SysDriver.h"
#include "CCompressDlg.h"


#ifdef _WIN64
#pragma comment(lib,"../x64/Debug\\DelayedLoading.lib")
#else
#pragma comment(lib,"../Debug\\DelayedLoading.lib")
#endif
#pragma warning(disable:4996)

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

extern "C" __declspec(dllimport) void FunOfDelayedLoading(CString &csStr);


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持
	
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}




// CHackSkillDlg 对话框



CHackSkillDlg::CHackSkillDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_HACKSKILL_DIALOG, pParent)
	, dwSecond(0)
	, csDllDelayedLoaded(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHackSkillDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_STATIC_Second, dwSecond);
	DDX_Text(pDX, IDC_STATIC_DllDelayedLoading, csDllDelayedLoaded);
}

BEGIN_MESSAGE_MAP(CHackSkillDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_FreeRes, &CHackSkillDlg::OnBnClickedButtonFreeres)
	ON_BN_CLICKED(IDC_BUTTON_RemoteThreadInjection, &CHackSkillDlg::OnBnClickedButtonRemotethreadinjection)
	ON_BN_CLICKED(IDC_BUTTON_ServiceLoaded, &CHackSkillDlg::OnBnClickedButtonServiceloaded)
	ON_BN_CLICKED(IDC_BUTTON_GlobalHook, &CHackSkillDlg::OnBnClickedButtonGlobalhook)
	ON_BN_CLICKED(IDC_BUTTON_DriverSaveOn, &CHackSkillDlg::OnBnClickedButtonDriversaveon)
	ON_BN_CLICKED(IDC_BUTTON_DriverSaveOff, &CHackSkillDlg::OnBnClickedButtonDriversaveoff)
	ON_BN_CLICKED(IDC_BUTTON_DllMemRun, &CHackSkillDlg::OnBnClickedButtonDllmemrun)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_PuppetProcess, &CHackSkillDlg::OnBnClickedButtonPuppetprocess)
	ON_BN_CLICKED(IDC_BUTTON_CompressFunc, &CHackSkillDlg::OnBnClickedButtonCompressfunc)
END_MESSAGE_MAP()


// CHackSkillDlg 消息处理程序

BOOL CHackSkillDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	//Dll延迟加载
	csDllDelayedLoaded = L"DLL延迟加载  倒计时：";

	//倒计时秒数初始化
	dwSecond = 5;

	//更新数据到界面
	UpdateData(FALSE);

	//设置定时器调用延迟加载模块的导出函数
	SetTimer(1, 5000, NULL);
	SetTimer(2, 1000, NULL);

	//WindowsHook窗口
	m_WindowsHookDlg.Create(IDD_DIALOG_WindowsHookDlg);

	//内存加载运行窗口
	//m_DllMemRunDlg.Create(IDD_DIALOG_DllMemRunDlg);

	//获取驱动路径
	csDriverPath = GetDriverPath();

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CHackSkillDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
// 来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
// 这将由框架自动完成。

void CHackSkillDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}


//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CHackSkillDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}




//处理定时器消息
void CHackSkillDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	//倒计时减1
	dwSecond -= 1;

	//定时器到0，销毁定时器
	if (dwSecond == 0)
	{
		FunOfDelayedLoading(csDllDelayedLoaded);
		KillTimer(1);
		KillTimer(2);
	}

	//更新数据到界面
	UpdateData(FALSE);

	CDialogEx::OnTimer(nIDEvent);
}


//资源释放
BOOL FunOfFreeResource(UINT ResName, TCHAR* pResType, TCHAR* pSaveName)
{

	//定位我们的自定义资源，这里因为我们是从本模块定位资源，所以将句柄简单地置为NULL即可
	HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(ResName), pResType);

	if (NULL == hRsrc)
		return FALSE;

	//获取资源的大小
	DWORD dwSize = SizeofResource(NULL, hRsrc);

	if (0 == dwSize)
		return FALSE;

	//加载资源
	HGLOBAL hGlobal = LoadResource(NULL, hRsrc);

	if (NULL == hGlobal)
		return FALSE;

	//锁定资源
	LPVOID pBuffer = LockResource(hGlobal);

	if (NULL == pBuffer)
		return FALSE;

	BOOL bRt = FALSE;

	//创建文件
	FILE* fp = _tfopen(pSaveName, _T("wb"));

	if (fp != NULL)
	{
		if (dwSize == fwrite(pBuffer, sizeof(char), dwSize, fp))
			bRt = TRUE;
		fclose(fp);
	}

	//释放资源
	FreeResource(hGlobal);
	return bRt;
}


//资源释放buttom
void CHackSkillDlg::OnBnClickedButtonFreeres()
{
	// TODO: 在此添加控件通知处理程序代码
	FunOfFreeResource(IDR_MYCOMPRESSDLL2, L"MyCompressDll", L"MyResFree.myzip");
}


//打开进程管理管理窗口，选择注入方式
void CHackSkillDlg::OnBnClickedButtonRemotethreadinjection()
{
	// TODO: 在此添加控件通知处理程序代码
	//远程线程注入
	CMyInjectionDlg m_MyInjectionDlg;
	m_MyInjectionDlg.DoModal();
}


//服务加载窗口
void CHackSkillDlg::OnBnClickedButtonServiceloaded()
{
	// TODO: 在此添加控件通知处理程序代码
	//服务加载
	CServerLoadDlg m_CServerLoadDlg;
	m_CServerLoadDlg.DoModal();
}


void CHackSkillDlg::OnBnClickedButtonGlobalhook()
{
	// TODO: 在此添加控件通知处理程序代码
	//显示WindowsHook窗口
	m_WindowsHookDlg.ShowWindow(SW_SHOW);
}


//获取驱动路径
CString CHackSkillDlg::GetDriverPath()
{
	//定义路径变量
	wchar_t szFullPath[MAX_PATH] = { 0 };
	//获取当前模块路径
	GetModuleFileName(NULL, szFullPath, MAX_PATH);
	//保存为CString
	CString csDriverPath = szFullPath;
	//截取路径
	csDriverPath = csDriverPath.Left(csDriverPath.ReverseFind('\\'));
	//添加文件名
	csDriverPath += "\\MySSDTHook.sys";
	//返回驱动路径
	return  csDriverPath;
}

//判断系统是32位还是64位
BOOL Is64bitSystem()
{
	SYSTEM_INFO si;
	GetNativeSystemInfo(&si);
	if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
		si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
		return TRUE;
	else
		return FALSE;
}


void CHackSkillDlg::OnBnClickedButtonDriversaveon()
{
	// TODO: 在此添加控件通知处理程序代码
	OSVERSIONINFO osver = { sizeof(OSVERSIONINFO) };
	GetVersionEx(&osver);

	//win7	32位
	if ((!Is64bitSystem()) && osver.dwMajorVersion == 6 && osver.dwMinorVersion == 1)
	{
		Service m_Service(csDriverPath.GetBuffer());
		string error;
		m_Service.load(&error);
		if (m_Service.start(&error))
		{
			MessageBox(L"开启驱动保护成功!", L"提示");
		}
		else
		{
			MessageBoxA(NULL, error.c_str(), "错误", NULL);
		}
	}
	else
	{
		MessageBox(L"暂时只支持win7 32位系统!", L"提示");
	}
}


void CHackSkillDlg::OnBnClickedButtonDriversaveoff()
{
	// TODO: 在此添加控件通知处理程序代码
	Service m_Service(csDriverPath.GetBuffer());
	string error;

	if (m_Service.stop(&error))
	{
		if (m_Service.unLoad(&error))
		{
			MessageBox(L"关闭驱动保护成功!", L"提示");
		}
		else
		{
			MessageBoxA(NULL, error.c_str(), "错误", NULL);
		}
	}
	else
	{
		MessageBoxA(NULL, error.c_str(), "错误", NULL);
	}
}


//内存加载运行
void CHackSkillDlg::OnBnClickedButtonDllmemrun()
{
	// TODO: 在此添加控件通知处理程序代码
	//m_DllMemRunDlg.ShowWindow(SW_SHOW);
	CDllMemRunDlg m_DllMemRunDlg;
	m_DllMemRunDlg.DoModal();
}



//傀儡进程
void CHackSkillDlg::OnBnClickedButtonPuppetprocess()
{
	// TODO: 在此添加控件通知处理程序代码
	WinExec("PuppetProcess.exe",NULL);
}

//压缩功能
void CHackSkillDlg::OnBnClickedButtonCompressfunc()
{
	// TODO: 在此添加控件通知处理程序代码
	CCompressDlg m_CompressDlg;
	m_CompressDlg.DoModal();
}
